//
//  CanchasViewController.m
//  Futbol5Final
//
//  Created by Johan Chaves on 4/14/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CanchasViewController.h"
#import "CanchaCreator.h"
#import "CanchaVO.h"

@implementation CanchasViewController

@synthesize canchas,reservacionesViewController;

-(id) initWithCanchas :(LocalVO*)plocal:(NSDictionary *) canchasData{
    [canchas dealloc];
    [canchas release];
    
    self = [[[CanchasViewController alloc] initWithNibName:@"CanchasViewController" bundle:nil] autorelease];
    
    
    CanchaCreator *canchaCreator = [[CanchaCreator alloc]init];
    
    
    
    canchas = [canchaCreator createCanchas:plocal:canchasData];
    
    return self;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [canchas count];
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cancha";
    
    CanchaVO *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [canchas objectAtIndex:indexPath.row];
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CanchaVO *canchaS = (CanchaVO*)[tableView cellForRowAtIndexPath:indexPath];        
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
	    
        self.reservacionesViewController = [[ReservacionesViewController alloc]initWithReservaciones:canchaS :[canchaS reservaciones]];	
        
        [self.navigationController pushViewController:self.reservacionesViewController animated:YES];
    }  
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Canchas";
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end